<template>
    <div id="init-viewer-wrapper">
    </div>
</template>
  
<script>
export default {
    name: 'ArcgisMapServer',
    components: {},
    mounted() {
        this.initViewer()
    },
    methods: {
        async initViewer() {
            Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzM2RjYjJlOC01ZTQwLTQwODYtOTEwMy02M2U4OGEzYjQyNGUiLCJpZCI6MjI5NjUsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1ODI0NTYwMzF9.VG2_T9ry8EnBWAh4msJ3s6m2jW_5hgAZQvfEQDh-WQs"
            window.viewer = new window.Cesium.Viewer("init-viewer-wrapper", {
                terrainProvider: new window.Cesium.CesiumTerrainProvider({
                    url: 'http://data.mars3d.cn/terrain',
                    requestWaterMask: true,
                    requestVertexNormals: true
                }),
                sceneMode: window.Cesium.SceneMode.SCENE3D,
                vrButton: false,
                animation: false,
                baseLayerPicker: false,
                geocoder: false,
                timeline: false,
                fullscreenButton: false,
                homeButton: false,
                creditContainer: document.createElement('div'),
                infoBox: true,
                navigationHelpButton: false,
                sceneModePicker: false,
                scene3DOnly: true,
                skyBox: new window.Cesium.SkyBox({
                    sources: {
                        positiveX: require('@/assets/images/tycho2t3_80_px.jpg'),
                        negativeX: require('@/assets/images/tycho2t3_80_mx.jpg'),
                        positiveY: require('@/assets/images/tycho2t3_80_py.jpg'),
                        negativeY: require('@/assets/images/tycho2t3_80_my.jpg'),
                        positiveZ: require('@/assets/images/tycho2t3_80_pz.jpg'),
                        negativeZ: require('@/assets/images/tycho2t3_80_mz.jpg'),
                    }
                })
            });
            window.viewer.scene.globe.depthTestAgainstTerrain = true;


            let imageryProvider = new window.Cesium.ArcGisMapServerImageryProvider({
                url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer',
                layers: '4',
                // rectangle:window.Cesium.Rectangle.fromDegrees(117.26344486210633, 36.67686347861695, 117.273444862106332,  36.68686347861695),
                maximumLevel: 18,
                enablePickFeatures: true
            })
            console.log(imageryProvider)
            window.viewer.imageryLayers.addImageryProvider(imageryProvider)
            window.viewer.camera.setView({
                destination: window.Cesium.Cartesian3.fromDegrees(117.16978315861392, 36.93205961529747, 2000)
            });
            window.viewer.screenSpaceEventHandler.setInputAction(async function onLeftClick(movement) {
                // 获取鼠标点击位置的屏幕坐标
                var position = movement.position;
                console.log(window.viewer.scene.pickPosition(position))
                let cartographic = window.Cesium.Cartographic.fromCartesian(window.viewer.scene.pickPosition(position))
                // 使用pickFeatures方法获取选择的要素信息
                var promise = imageryProvider.pickFeatures(position.x, position.y, 18, cartographic.longitude,cartographic.latitude);
                let features = await promise
                console.log(features)
            }, window.Cesium.ScreenSpaceEventType.LEFT_CLICK);
        }
    }
}
</script>
  
<style>
#init-viewer-wrapper {
    width: 100%;
    height: 100%;
}
</style>
  